package org.jim.server.demo.service;

import org.jim.core.ImChannelContext;
import org.jim.core.http.HttpRequest;
import org.jim.core.packets.*;
import org.jim.core.ws.WsRequestPacket;
import org.jim.server.demo.command.WebWsHandshakeProcessor;
import org.jim.server.processor.chat.BaseAsyncChatMessageProcessor;
import org.jim.server.processor.login.LoginCmdProcessor;
import org.jim.server.protocol.AbstractProtocolCmdProcessor;
import org.springframework.context.annotation.Bean;

public abstract class AbstactTransformHandler {

    /**
     * 调用链路
     *  loginHake -> getLoginReqBody -> doLogin -> getUser -> onSuccess -> doChatMessageProcessor
     */

    @Bean
    public void initHandlerMapper(){
        InitHandlerMapper.handlerMapper.put(InitHandlerMapper.HANDLER_CONFIG,this);
        initHandler();
    }

    public final void initHandler() {
        TransformHandler.setHandler(new WebAsyncChatMessageProcessor());
        TransformHandler.setHandler(new DefultWebWsHandshakeProcessor());
        TransformHandler.setHandler(new WebLoginServiceProcessor());
    }

    /**
     * 处理消息
     *
     * @param chatBody
     * @param imChannelContext
     */
    public void doChatMessageProcessor(ChatBody chatBody, ImChannelContext imChannelContext){

    };

    /**
     * 获取存储的消息
     * 重写了这个可以登录上后自动发消息
     *
     * @param user
     * @param imChannelContext
     */
    public void doAfterBind(ImChannelContext imChannelContext, User user){

    };

    /**
     * 获取存储的消息
     * 重写了这个可以登录上后自动发消息
     *
     * @param group
     * @param imChannelContext
     */
    public void doAfterBind(ImChannelContext imChannelContext, Group group){

    }

    /**
     * 获取验证true 通过
     *
     * @param request
     */
    public Boolean webLoginHake(WsRequestPacket request){
        return null;
    };

    /**
     * 返回登录人账号，密码，token
     *
     * @param request
     */
    public LoginReqBody afterWebHakeGetLoginReqBody(HttpRequest request){
        return null;
    };

    /**
     * 获取登录人信息
     *
     * @param loginReqBody
     * @param imChannelContext
     */
    public User getWebUser(LoginReqBody loginReqBody, ImChannelContext imChannelContext){
        return null;
    };

    /**
     * 登陆成功返回状态码:ImStatus.C10007
     * 登录失败返回状态码:ImStatus.C10008
     * 注意：只要返回非成功状态码(ImStatus.C10007),其他状态码均为失败,此时用户可以自定义返回状态码，定义返回前端失败信息
     *
     * @param loginReqBody
     * @param imChannelContext
     */
    public LoginRespBody doWebLogin(LoginReqBody loginReqBody, ImChannelContext imChannelContext){
        return null;
    };

    /**
     * 登录成功回调方法
     *
     * @param user
     * @param channelContext
     */
    public void onWebLoginSuccess(User user, ImChannelContext channelContext){
    };

    /**
     * 登录失败回调方法
     *
     * @param imChannelContext
     */
    public void onWebLoginFailed(ImChannelContext imChannelContext){

    };

    class WebAsyncChatMessageProcessor extends BaseAsyncChatMessageProcessor {
        @Override
        public void doProcess(ChatBody chatBody, ImChannelContext imChannelContext) {
            doChatMessageProcessor(chatBody, imChannelContext);
        }
    }

    class WebLoginServiceProcessor extends AbstractProtocolCmdProcessor implements LoginCmdProcessor {

        @Override
        public User getUser(LoginReqBody loginReqBody, ImChannelContext imChannelContext) {
            User user = getWebUser(loginReqBody, imChannelContext);
            return user;
        }

        @Override
        public LoginRespBody doLogin(LoginReqBody loginReqBody, ImChannelContext imChannelContext) {
            LoginRespBody body = doWebLogin(loginReqBody, imChannelContext);
            return body;
        }

        @Override
        public void onSuccess(User user, ImChannelContext channelContext) {
            onWebLoginSuccess(user, channelContext);
        }

        @Override
        public void onFailed(ImChannelContext imChannelContext) {
            onWebLoginFailed(imChannelContext);
        }
    }

    class DefultWebWsHandshakeProcessor extends WebWsHandshakeProcessor {

        @Override
        public boolean loginHake(WsRequestPacket request) {
            Boolean b = webLoginHake(request);
            if(b == null){
                return super.loginHake(request);
            }
            return b.booleanValue();
        }

        @Override
        public LoginReqBody getLoginReqBody(HttpRequest request) {
            LoginReqBody reqBody = afterWebHakeGetLoginReqBody(request);
            if(reqBody == null) {
                return super.getLoginReqBody(request);
            }
            return reqBody;
        }
    }
}
